using System;
using Common.Data.Hibernate;
using NHibernate;
using NHibernate.Cfg;

namespace FwkLight.Persistence
{
  public interface ISessionSource
  {
    ISession CreateSession ( );
  }

  public class SessionSource : ISessionSource
  {
    private ISessionFactory _sessionFactory;

    public ISession CreateSession ( )
    {
      if (_sessionFactory != null)
        return _sessionFactory.OpenSession();

      Configuration configuration = NHibernateUtils.LoadDefaultConfiguration();
      _sessionFactory = configuration.BuildSessionFactory();
      return _sessionFactory.OpenSession();
    }
  }
}